package com.chinasofti.admin.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.chinasofti.manager.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Creator: <br>
 * Date: <br>
 * Description: <br>
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Reference
    private AdminService adminService;

    @PostMapping("/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        RedirectAttributes attributes){
        boolean isSuccess = adminService.login(username, password);
        if (isSuccess){
            return "redirect:/admin";
        }else {
            attributes.addFlashAttribute("msg", "密码错误");
            return "redirect:/admin/login";
        }
    }

    @GetMapping("/login")
    public String login() {
        return "login";
    }

    @GetMapping("")
    public String home() {
        return "index";
    }

}
